package com.wu.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;

public class HttpUtil {
	
	 /**
     * 向指定URL发送GET方法的请求
     * 
     * @param url
     *            发送请求的URL
     * @param param
     *            请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
     * @return URL 所代表远程资源的响应结果
     */
    public static String sendGet(String urlString) {
    	String body="";
    	HttpURLConnection httpUrlConn = null;
    	InputStreamReader inputStreamReader=null;
        //System.out.println(monitorInfo.getDomainUrl()+"====");
        try {
            URL url = new URL(urlString);
            httpUrlConn = (HttpURLConnection) url.openConnection();
            httpUrlConn.setRequestProperty("Accept-Charset", "utf-8");
            httpUrlConn.setRequestProperty("Content-Type", "application/json");
            httpUrlConn.setRequestProperty("Connection", "keep-alive");
            httpUrlConn.setDoOutput(true); // http正文内，因此需要设为true, 默认情况下是false;
            httpUrlConn.setDoInput(true); // 设置是否从httpUrlConnection读入，默认情况下是true;
            httpUrlConn.setUseCaches(false); // Post 请求不能使用缓存
            httpUrlConn.setRequestMethod("GET");
            httpUrlConn.connect();
            // 将返回的输入流转换成字符串
            inputStreamReader = new InputStreamReader(
                    httpUrlConn.getInputStream(),"UTF-8");
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String str = null;
            StringBuffer buffer = new StringBuffer();
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
            }
            body=buffer.toString();
        }catch (Exception e) {
			// TODO: handle exception
		}
        // 使用finally块来关闭输入流
        finally {
        	try {
				inputStreamReader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
        return body;
    }
    
    public static String sendGetIO(String urlString) {
    	String body="";
    	HttpURLConnection httpUrlConn = null;
    	InputStreamReader inputStreamReader=null;
        //System.out.println(monitorInfo.getDomainUrl()+"====");
        try {
            URL url = new URL(urlString);
            httpUrlConn = (HttpURLConnection) url.openConnection();
            httpUrlConn.setRequestProperty("Accept-Charset", "utf-8");
            httpUrlConn.setRequestProperty("Content-Type", "application/json");
            httpUrlConn.setRequestProperty("Connection", "keep-alive");
            httpUrlConn.setDoOutput(true); // http正文内，因此需要设为true, 默认情况下是false;
            httpUrlConn.setDoInput(true); // 设置是否从httpUrlConnection读入，默认情况下是true;
            httpUrlConn.setUseCaches(false); // Post 请求不能使用缓存
            httpUrlConn.setRequestMethod("GET");
            httpUrlConn.connect();
            // 将返回的输入流转换成字符串
            inputStreamReader = new InputStreamReader(
                    httpUrlConn.getInputStream(),"UTF-8");
            BufferedReader bufferedReader = new BufferedReader(
                    inputStreamReader);
            String str = null;
            StringBuffer buffer = new StringBuffer();
            while ((str = bufferedReader.readLine()) != null) {
                buffer.append(str);
                System.out.println(buffer);
            }
            File videoFile = new File("d:/2.mp4"); 

            FileOutputStream outStream = new FileOutputStream(videoFile); 


            outStream.write(buffer.toString().getBytes());

            outStream.close();
            
            //body=buffer.toString();
        }catch (Exception e) {
			// TODO: handle exception
		}
        // 使用finally块来关闭输入流
        finally {
        	try {
				inputStreamReader.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
        return body;
    }

}
